package com.jxw.launcher;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import com.jxw.constant.Constant;
import com.jxw.engine.platsign.MainActivity;
import com.jxw.launcher.util.SharedPreferencesUtil;
import com.jxw.launcher.view.AutoPollRecyclerView;
import com.jxw.launcher.view.XdfVideoPlayer;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONObject;

/* loaded from: classes.dex */
public class MyFragmentXdf extends BaseFragment {
    private static final String SP_VOICE = "voice";
    private static final String TAG = "XdfFragment";
    Handler handler = new Handler() { // from class: com.jxw.launcher.MyFragmentXdf.1
        @Override // android.os.Handler
        public void handleMessage(Message message) {
            super.handleMessage(message);
            if (TextUtils.isEmpty(MyFragmentXdf.this.mp4Url)) {
                return;
            }
            MyFragmentXdf.this.player.start(MyFragmentXdf.this.mp4Url);
        }
    };
    private boolean isMute;
    private Switch ivVoice;
    String mp4Url;
    private XdfVideoPlayer player;
    private AutoPollRecyclerView recyclerView;
    SharedPreferences sharedPreferences;

    @Override // android.support.v4.app.Fragment
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflater = inflater(layoutInflater, viewGroup, R.layout.fragment_xdf);
        initView(inflater);
        return inflater;
    }

    @Override // android.support.v4.app.Fragment
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ");
        playVideo();
    }

    @Override // android.support.v4.app.Fragment
    public void onPause() {
        Log.d(TAG, "onPause: ");
        stopVideo();
        super.onPause();
    }

    @Override // android.support.v4.app.Fragment
    public void onStop() {
        Log.d(TAG, "onStop: ");
        releaseVideo();
        super.onStop();
    }

    @Override // android.support.v4.app.Fragment
    public void setUserVisibleHint(boolean z) {
        super.setUserVisibleHint(z);
        Log.d(TAG, "setUserVisibleHint: isVisibleToUser=" + z);
        if (z) {
            playVideo();
        } else {
            stopVideo();
        }
    }

    private void initView(View view) {
        this.ivVoice = (Switch) view.findViewById(R.id.iv_voice);
        this.player = (XdfVideoPlayer) view.findViewById(R.id.video_player);
        this.isMute = SharedPreferencesUtil.getBoolean(requireContext(), SP_VOICE, false).booleanValue();
        this.ivVoice.setChecked(!this.isMute);
        updateVoice();
        this.recyclerView = (AutoPollRecyclerView) view.findViewById(R.id.rcv_book);
        this.ivVoice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { // from class: com.jxw.launcher.MyFragmentXdf.2
            @Override // android.widget.CompoundButton.OnCheckedChangeListener
            public void onCheckedChanged(CompoundButton compoundButton, boolean z) {
                if (z) {
                    MyFragmentXdf.this.isMute = false;
                } else {
                    MyFragmentXdf.this.isMute = true;
                }
                SharedPreferencesUtil.setBoolean(MyFragmentXdf.this.requireContext(), MyFragmentXdf.SP_VOICE, Boolean.valueOf(MyFragmentXdf.this.isMute));
                MyFragmentXdf.this.updateVoice();
            }
        });
        addBookData();
    }

    private void addBookData() {
        ArrayList arrayList = new ArrayList();
        arrayList.add(new BookBean(R.drawable.xdfzq_sb1, 23151, "西游记"));
        arrayList.add(new BookBean(R.drawable.xdfzq_sb2, 23127, "居里夫人传"));
        arrayList.add(new BookBean(R.drawable.xdfzq_sb3, 23120, "安徒生童话选（拼音版或绘本）"));
        arrayList.add(new BookBean(R.drawable.xdfzq_sb4, 23154, "鲁滨逊漂流记"));
        arrayList.add(new BookBean(R.drawable.xdfzq_sb5, 23146, "福尔摩斯探案集"));
        AutoPollAdapter autoPollAdapter = new AutoPollAdapter(requireContext(), arrayList);
        this.recyclerView.setLayoutManager(new LinearLayoutManager(requireContext(), 0, false));
        this.recyclerView.addItemDecoration(new DividerItemDecoration(requireContext(), 0));
        this.recyclerView.setAdapter(autoPollAdapter);
        this.recyclerView.start();
    }

    private void playVideo() {
        XdfVideoPlayer xdfVideoPlayer;
        String str;
        if (getUserVisibleHint() && (xdfVideoPlayer = this.player) != null) {
            if (xdfVideoPlayer.getCurrentState() == 5) {
                this.player.resume();
                return;
            }
            if (!new File(Environment.getExternalStorageDirectory() + "/ansystem/新东方/xdf.mp4").exists()) {
                str = Environment.getExternalStorageDirectory() + "/ansystem/xdf/xdf.mp4";
            } else {
                str = Environment.getExternalStorageDirectory() + "/ansystem/新东方/xdf.mp4";
            }
            if (!new File(str).exists()) {
                this.sharedPreferences = getActivity().getSharedPreferences(MainActivity.PREFERENCE, 0);
                if (TextUtils.isEmpty(this.sharedPreferences.getString("token", ""))) {
                    return;
                }
                if (TextUtils.isEmpty(this.sharedPreferences.getString("xdf_videoUrl", ""))) {
                    getVideo(this.sharedPreferences.getString("token", ""));
                    return;
                }
                this.mp4Url = this.sharedPreferences.getString("xdf_videoUrl", "");
                if (TextUtils.isEmpty(this.mp4Url)) {
                    return;
                }
                this.player.start(this.mp4Url);
                return;
            }
            this.player.start(str);
        }
    }

    private void stopVideo() {
        XdfVideoPlayer xdfVideoPlayer = this.player;
        if (xdfVideoPlayer == null) {
            return;
        }
        if (xdfVideoPlayer.getCurrentState() == 2) {
            this.player.pause();
        } else {
            this.player.release();
        }
    }

    private void releaseVideo() {
        XdfVideoPlayer xdfVideoPlayer = this.player;
        if (xdfVideoPlayer == null) {
            return;
        }
        xdfVideoPlayer.release();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void updateVoice() {
        this.player.setMute(this.isMute);
    }

    private void getVideo(String str) {
        try {
            new OkHttpClient().newCall(new Request.Builder().addHeader("token", str).url(Constant.PLATFORM_URL_HEADER + "/api/recommend/word/list?name=xdf").get().build()).enqueue(new Callback() { // from class: com.jxw.launcher.MyFragmentXdf.3
                @Override // okhttp3.Callback
                public void onFailure(Call call, IOException iOException) {
                    Log.d("zsj", "sendPost onFailure=" + iOException.getMessage());
                }

                @Override // okhttp3.Callback
                public void onResponse(Call call, Response response) {
                    try {
                        JSONObject jSONObject = new JSONObject(response.body().string());
                        if ("0".equals(jSONObject.getString("code"))) {
                            JSONObject jSONObject2 = jSONObject.getJSONObject("data");
                            MyFragmentXdf.this.mp4Url = jSONObject2.getString("mp4Url");
                            if (TextUtils.isEmpty(MyFragmentXdf.this.sharedPreferences.getString("token", "")) || !TextUtils.isEmpty(MyFragmentXdf.this.sharedPreferences.getString("xdf_videoUrl", ""))) {
                                return;
                            }
                            SharedPreferences.Editor edit = MyFragmentXdf.this.sharedPreferences.edit();
                            edit.putString("xdf_videoUrl", MyFragmentXdf.this.mp4Url);
                            edit.commit();
                            MyFragmentXdf.this.handler.sendEmptyMessage(0);
                        }
                    } catch (Exception e) {
                        e.getMessage();
                    }
                }
            });
        } catch (Exception e) {
            e.getMessage();
        }
    }
}
